 //
//  TSYTabbarViewController.swift
//  TSYWeiBoDemo
//
//  Created by apple on 2016/11/23.
//  Copyright © 2016年 apple. All rights reserved.
//

import UIKit

class TSYTabbarViewController: UITabBarController {
    
    private var timer:Timer?

    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupChildController()
        setupTimer()
        // Do any additional setup after loading the view.
        
        //通知监听 
        NotificationCenter.default.addObserver(self, selector: #selector(userLogin), name: NSNotification.Name(rawValue: notiLogin), object: nil)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    override var supportedInterfaceOrientations: UIInterfaceOrientationMask{
        
        return .portrait
    }
    
    deinit {
        timer?.invalidate()
        
        NotificationCenter.default.removeObserver(self)
    }
    
    private func setupTimer(){
        
        timer = Timer.scheduledTimer(timeInterval: 5, target: self, selector: #selector(unreadInfo), userInfo: nil, repeats: true)
    }
    
    @objc private func unreadInfo(){
    
            print("未读信息")
    }
    
    @objc private func userLogin(n:Notification){
        
        print(n)
    }
}
 
 //MARK:UITabBarControllerDelegate
 extension TSYTabbarViewController :UITabBarControllerDelegate{
    
    
    func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
        
        //获取控制器索引
        let inx = (childViewControllers as NSArray).index(of: viewController)
        
        //判断点按的按钮是哪一个试图的
        
        if selectedIndex == 0 && inx == selectedIndex {
            print("点击了首页")
            
            let nvc = childViewControllers[0] as! UINavigationController
            let vc = nvc.childViewControllers[0] as! TSYHomeViewController
            
            vc.tableView?.setContentOffset(CGPoint(x: 0, y: -64), animated: true)
            
            DispatchQueue.main.asyncAfter(deadline: DispatchTime.now()+1, execute: { 
                print("刷新数据 ")
            })
        }
        
        return !viewController.isMember(of: UIViewController.self)
    }
    
    
    
 }

 
extension TSYTabbarViewController{
    
    func setupChildController(){
        
//        guard let path = Bundle.main.path(forResource: "main.json", ofType: nil) else {
//            return
//        }
//        
//        let arrays = NSData(contentsOfFile:path)
//        
//        let ar = try? JSONSerialization.jsonObject(with: arrays as! Data, options: []) as? [[String:AnyObject]]
        
        let array = [["clsName":"TSYHomeViewController","title":"home","imageName":"1"],["clsName":"TSYHomeViewController","title":"home","imageName":"1"],["clsName":"TSYHomeViewController","title":"home","imageName":"1"]]
        
        var arrayVC = [UIViewController]()
        for dict in array{
            
            arrayVC.append(controller(dict: dict))
        }
        viewControllers = arrayVC
        
    }
    
    
    /// 字典创建控制器
    ///
    /// - Parameter dict: 包含所有试图的字典
    /// - Returns: 一个试图
    private func controller(dict:[String:String])->UIViewController{
        
        guard let clsName = dict["clsName"],
            let title = dict["title"],
            let imageName = dict["imageName"],
            let cls = NSClassFromString(Bundle.main.namespace+"."+clsName) as? UIViewController.Type else {
            
            return UIViewController()
        }
        
        let vc = cls.init()
        vc.title = title
        
        vc.tabBarItem.image = UIImage(named: ""+imageName)
        
        vc.tabBarItem.selectedImage = UIImage(named: ""+imageName)?.withRenderingMode(.alwaysOriginal)
        
        let nvc = TSYNavigationController(rootViewController: vc)
        
        return nvc
        
    }
    
    
    /// 正则表达式
    func zhengzhe() {
        
        let str = "<a href=\"http://baidu.com\" rel=\"nofo\"> 微博 weibo.com</a>"
        
        //1.创建正则表达式
        
//        let regx = try? Regular
        
        
        
        
        
    }
    
    
    
    
    
    
    
}
